package com.tourism.www;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.t2cn.common.www.ServletCommon;
import com.tourism.storage.dao.CourseDao;
import com.tourism.storage.dao.PartnerDao;
import com.tourism.storage.pojo.Courses;
import com.tourism.storage.pojo.Partner;
import com.tourism.www.common.TourismBaseHandler;

public class CoursesHandler extends TourismBaseHandler {

	@Override
	public String doActionX(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String method = getMethodName(request);
		if( "add".equals(method) ) {
			return addCourse(request, response);
		} else if("list".equals(method)){
			return list(request, response);
		} else if("page_add".equals(method)) {
			return "page_add";
		} else if("delete".equals(method)) {
			return deleteCourse(request, response);
		} else if("getCourseById".equals(method)) {
			return getCourseById(request, response);
		} else if("update".equals(method)) {
			return updateCourse(request, response);
		}
		
		return null;
	}
	
	private String addCourse(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		String coursename = ServletCommon.getParamValue(request, "coursename");
		String coursecode = ServletCommon.getParamValue(request, "coursecode");
		Integer coursetype = ServletCommon.getIntParamValue(request, "coursetype", 0);
		Integer price = ServletCommon.getIntParamValue(request, "price", 0);
		
		Courses	pojo = new Courses();
		pojo.setCoursename(coursename);
		pojo.setCoursecode(coursecode);
		pojo.setCoursetype(coursetype);
		pojo.setPrice(price);
		pojo.setStatus(0);
		
		CourseDao dao = new CourseDao();
		dao.addCourse(pojo);
		
		return "list";
	}

	private String updateCourse(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer courseid = ServletCommon.getIntParamValue(request, "courseid", 0);
		String coursename = ServletCommon.getParamValue(request, "coursename");
		String coursecode = ServletCommon.getParamValue(request, "coursecode");
		Integer coursetype = ServletCommon.getIntParamValue(request, "coursetype", 0);
		Integer price = ServletCommon.getIntParamValue(request, "price", 0);
		
		Courses pojo = new Courses();
		pojo.setCourseid(courseid);
		pojo.setCoursename(coursename);
		pojo.setCoursecode(coursecode);
		pojo.setCoursetype(coursetype);
		pojo.setPrice(price);
		CourseDao dao = new CourseDao();
		dao.updateCourse(pojo);
		
		return "list";
	}
	
	private String list(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		CourseDao dao = new CourseDao();
		List<Courses> list = dao.getCourses(0);
		request.setAttribute("courses", list);
		return "page_list";
	}
	
	private String getCourseById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer courseid = ServletCommon.getIntParamValue(request, "courseid", 0);
System.out.print("-------------courseid="+courseid);
		CourseDao dao = new CourseDao();
		Courses pojo = dao.getCourseById(courseid);
		request.setAttribute("pojo", pojo);
		return "page_update";
	}
	
	private String deleteCourse(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer courseid = ServletCommon.getIntParamValue(request, "courseid", 0);

		//String address = ServletCommon.getParamValue(request, "address");
		Courses pojo = new Courses();
		pojo.setCourseid(courseid);
		pojo.setStatus(1);
		CourseDao dao = new CourseDao();
		dao.updateCourse(pojo);
		
		return "list";
	}
}
